<%@ page import="java.sql.ResultSet" %>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%@include file="/common/config.jsp" %>
<html>
<head>
</head>
<body>
<center>
    <%

        request.setCharacterEncoding("UTF-8");
        //4、创键sql命令（给司机下指令）
        String dataSql = "select * from t_user where 1=1 ";
        String countSql = "select count(0) as count from t_user where 1=1 ";

        String where = "";
        String sno = request.getParameter("sno");
        if (sno != null && !"".equals(sno)) {
            where = where + " AND  id like '%" + sno + "%'";
        } else {
            sno = "";
        }
        String name = request.getParameter("name");
        if (name != null && !"".equals(name)) {
            where = where + " AND name like '%" + name + "%'";
        } else {
            name = "";
        }
        String clazz = request.getParameter("clazz");
        if (clazz != null && !"".equals(clazz)) {
            where = where + " AND clazz = '" + clazz + "'";
        } else {
            clazz = "";
        }

        int pageIndex = 1;
        int pageSize = 10;
        int maxCount = 0;
        int maxPageIndex = 0;
    %>
    <form action="query.jsp" method="post" id="form">
        <input type="hidden" name="pageIndex" id="pageIndex" value="1">
        请输入学号：<input type="text" name="sno" value="<%=sno%>">
        请输入姓名:<input type="text" name="name" value="<%=name%>">
        请选择班级：<select name="clazz">
        <option value="">全部</option>
        <option value="18电子商务技术1班" <%=clazz.equals("18电子商务技术1班") ? "selected" : ""%>>18电子商务技术1班</option>
        <option value="18电子商务技术2班" <%=clazz.equals("18电子商务技术2班") ? "selected" : ""%>>18电子商务技术2班</option>
        <option value="18计算机应用技术（数据应用）" <%=clazz.equals("18计算机应用技术（数据应用）") ? "selected" : ""%>>18计算机应用技术（数据应用）</option>
    </select>
        <input type="submit" value="查询">
    </form>
    <input type="button" value="添加" onclick="add();">
    <input type="button" value="删除" onclick="del();">
    <table border="1px" width="930px">
        <tr>
            <td width="30px">
                <input type="checkbox" id="checkAll" onchange="checkAll();">全选
            </td>
            <td width="150px">学号</td>
            <td width="150px">姓名</td>
            <td width="300px">班级</td>
            <td width="200px">操作</td>
        </tr>
        <%


            try {
                pageIndex = Integer.parseInt(request.getParameter("pageIndex"));
            } catch (Exception ex) {
                pageIndex = 1;
            }
            dataSql = dataSql + where + "limit " + (pageIndex - 1) * pageSize + "," + pageSize;

            countSql = countSql + where;
            ResultSet countRs = stmt.executeQuery(countSql);
            if (countRs.next()) {
                maxCount = countRs.getInt("count");
            }

            if (maxCount % pageSize == 0) {
                maxPageIndex = maxCount / pageSize;
            } else {
                maxPageIndex = (maxCount / pageSize) + 1;
            }


            //5、执行命令（司机去拉货）
            ResultSet rs = stmt.executeQuery(dataSql);
            //6、处理结果集（卸货）
            while (rs.next()) {
                String id = rs.getString("id");
                out.print("<tr>");
                out.print("<td>");
                out.print("<input type=\"checkbox\" name='checkbox_id' value='" + id + "'> ");
                out.print("</td>");
                out.print("<td>");
                out.print(rs.getString("id"));
                out.print("</td>");
                out.print("<td>");
                out.print(rs.getString("name"));
                out.print("</td>");
                out.print("<td>");
                out.print(rs.getString("clazz"));
                out.print("</td>");
                out.print("<td>");
                out.print("<a href='edit.jsp?action=update&id=" + id + "'>修改</a>|");
                out.print("<a href='javascript:del(\"" + id + "\")'>删除</a>|");
                out.print("|<a href='edit.jsp?action=view&id=" + id + "'>查看详情</a>");
                out.print("</td>");
                out.print("</tr>");

            }
            //7、关闭资源（卸磨杀驴）
            conn.close();
        %>
        <tr align="center">
            <td colspan="5">
                <a href="javascript:pageQuery('1');">首页</a>|
                <%
                    if (pageIndex != 1) {
                %>
                <a href="javascript:pageQuery('<%=pageIndex-1<1?1:pageIndex-1%>')">上一页</a>|

                <%
                    }
                %>

                <%
                    if (pageIndex < maxPageIndex) {
                %>
                <a href="javascript:pageQuery('<%=pageIndex+1>maxPageIndex?maxPageIndex:pageIndex+1%>')">下一页</a>|
                <%
                    }
                %>
                <a href="javascript:pageQuery('<%=maxPageIndex%>')">尾页</a>
                （当前第<%=pageIndex%>页，共<%=maxCount%>条记录，共<%=maxPageIndex%>页）
            </td>
        </tr>

    </table>
</center>
<script type="application/javascript">

    var form = document.getElementById("form");
    var pageIndex = document.getElementById("pageIndex");


    function checkAll() {
        var checkAll = document.getElementById("checkAll");
        var checkAllids = document.getElementsByName("checkbox_id");
        for (var i = 0; i < checkAllids.length; i++) {
            checkAllids[i].checked = checkAll.checked;
        }
    }

    function del(id) {
        if (id) {
            if (confirm("你是否要删除学号为" + id + "的同学吗？")) {
                window.location = "saveOrUpdate.jsp?action=delete&id=" + id;
            } else {
                alert("你取消了删除学号为" + id + "的同学");
            }
        } else {
            var checkAllids = document.getElementsByName("checkbox_id");
            var delIds = '';
            for (var i = 0; i < checkAllids.length; i++) {
                if (checkAllids[i].checked) {
                    delIds += checkAllids[i].value + ',';
                }
            }
            if (delIds) {
                if (confirm("你是否要删除选中的同学吗？")) {
                    window.location = "saveOrUpdate.jsp?action=delete&id=" + delIds;
                } else {
                    alert("你取消了删除学号为" + id + "的同学");
                }
            } else {
                alert("请至少选择一个要删除的学生");
            }


        }
    }

    function add() {
        window.location = "edit.jsp?action=add";
    }

    function pageQuery(page) {
        pageIndex.value = page;
        form.submit();
    }

</script>


</body>
</html>

